package com.lanqiao.attendance.service.impl;

import com.lanqiao.attendance.mapper.*;
import com.lanqiao.attendance.pojo.Attendance;
import com.lanqiao.attendance.pojo.Class;
import com.lanqiao.attendance.pojo.Course;
import com.lanqiao.attendance.pojo.Student;
import com.lanqiao.attendance.service.CourseService;
import com.lanqiao.attendance.vo.CourseByStuVo;
import com.lanqiao.attendance.vo.CourseVo;
import com.lanqiao.attendance.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMaper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private CourseVoMapper courseVoMapper;


    @Override
    public Result queryById(Long id) {
        Course course = courseMaper.queryById(id);
        if (course != null) {
            return Result.success("查询成功", course);
        } else {
            return Result.fail(3001, "查询失败");
        }
    }

    @Override
    public Result query() {
        List<Course> courseList = courseMaper.query();
        if (courseList.size() > 0) {
            System.out.println(courseList);
            return Result.success("查询成功", courseList);
        } else {
            return Result.fail(3001, "查询失败");
        }
    }

    @Transactional
    @Override
    public Result insertBySchedule(CourseVo courseVo) {
        Course course = new Course();
        course.setCouClassId(courseVo.getCouClassId());
        course.setCouTeaId(courseVo.getCouTeaId());
        course.setCouNumber("Lq"+System.currentTimeMillis());
        course.setCouTitle(courseVo.getCouTitle());
        course.setCouDetail(courseVo.getCouDetail());
        course.setCouTimeLength(courseVo.getCouTimeLength());
        course.setCouStatus(courseVo.getCouStatus());
        course.setCouStartTime(courseVo.getCouStartTime());
        course.setCouEndTime(courseVo.getCouEndTime());
        //将新增课程加入到数据库
        int row = courseMaper.insert(course);
        if (row > 0) {
            //1.获取自增的课程id
            Long id = course.getId();
            //查询一下新增的课程的完整信息
            Course newCourse = courseMaper.queryById(id);
            System.out.println("*****************************************新增的课程为为：" + newCourse);
            //2.通过前端传回的courseVo中的className 查询该班级的所有学生
            List<Student> studentList = studentMapper.queryStuByClsName(courseVo.getClassName());
            //3.把每个学生的学生id和新增的课程id放在一起存入签到表中
            //新建一个集合，用于存放即将插入的学生签到信息
            List<Attendance> attendanceList = new ArrayList<>();
            for (Student student:studentList) {
                Attendance attendance = new Attendance();
                attendance.setStuId(student.getId());
                attendance.setCourseId(id);
                attendance.setStatus("0");
                attendanceList.add(attendance);
            }
            //传给mapper一个集合，进行批量插入
            attendanceMapper.batchInsert(attendanceList);
            return Result.success("添加成功",newCourse);
        } else {
            return Result.fail(3002,"添加失败");
        }
    }

    @Override
    public Result queryByCourseNum(String num) {
        Course course = courseMaper.queryByCourseNum(num);
        if (course != null) {
            return Result.success("查询成功", course);
        } else {
            return Result.fail(3001, "查询失败");
        }
    }

    @Override
    @Transactional
    public Result insert(Course course) {
        int row = courseMaper.insert(course);

        if (row > 0) {
            Long id = course.getId();
            Course newCourse = courseMaper.queryById(id);
            return Result.success("添加成功", newCourse);
        } else {
            return Result.fail(3002, "添加失败");
        }
    }

    @Override
    public Result delete(Long id) {
        int row = courseMaper.delete(id);
        if (row > 0) {
            Course deleteCourse = courseMaper.queryById(id);
            return Result.success("删除成功", deleteCourse);
        } else {
            return Result.fail(3003, "删除失败");
        }
    }

    @Override
    public Result update(Course course) {
        int row = courseMaper.update(course);
        if (row > 0) {
            Course updateCourse = courseMaper.queryById(course.getId());
            return Result.success("修改成功", updateCourse);
        } else {
            return Result.fail(3004, "修改失败");
        }
    }

    @Override
    public Result querySchedule(Long id) {
        List<CourseVo> courseVos = courseVoMapper.querySchedule(id);

            return Result.success("查询排课成功", courseVos);

    }

    @Override
    public List<CourseByStuVo> queryCourseStu(String stuAccount) {
        List<CourseByStuVo> courseByStuVos = courseMaper.queryCourseStu(stuAccount);
        return courseByStuVos;
    }

    @Override
    public List<CourseByStuVo> queryCourseStuByTitle(String StuAccount, String couTitle) {
        List<CourseByStuVo> courseByStuVo = courseMaper.queryCourseStuByTitle(StuAccount, couTitle);

        return courseByStuVo;
    }

    @Override
    public Result queryCourseDetail(String couNumber) {
        CourseByStuVo courseByStuVo = courseMaper.queryCourseDetail(couNumber);
        if (courseByStuVo != null) {
            return Result.success("查询成功", courseByStuVo);
        }
        return Result.fail(400, "查询失败");
    }
}
